//
//  ViewController.m
//  allTest
//
//  Created by 李培根 on 2018/11/2.
//  Copyright © 2018 李培根. All rights reserved.
//

#import "ViewController.h"
#import <YYCategories.h>
#import <Masonry.h>
#import "ZYStoryTransitiomScrollView.h"
#import "VideoPlayViewController.h"
#import "GPUViewController.h"

#define DEGREES_TO_RADIANS(x) ((x)/180.0*M_PI)
#define TARGET_ANGLE (M_PI_2 * 0.7)
@interface ViewController ()<UIScrollViewDelegate, UIPopoverPresentationControllerDelegate>

@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, strong) UIView *layerView1;
@property (nonatomic, strong) UIView *layerView2;
@property (nonatomic, strong) NSMutableArray *viewArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    btn.backgroundColor = [UIColor cyanColor];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)btnClick:(UIButton *)sender {
    UIStoryboard *mainSB = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    GPUViewController *selfdetailVC = [mainSB instantiateViewControllerWithIdentifier:@"GPUViewController"];
    [self.navigationController pushViewController:selfdetailVC animated:YES];
}

- (void)testMapTable {
    NSMapTable *table = [NSMapTable mapTableWithKeyOptions:NSPointerFunctionsCopyIn valueOptions:NSPointerFunctionsWeakMemory];
    [table setObject:[NSObject new] forKey:@1];
    [table setObject:[NSObject new] forKey:@2];
    for (id key in table) {
        NSLog(@"%@", key);
    }
    NSLog(@"%@", @(table.count));
}

- (void)testScrollView {
    NSArray *imageNameArray = @[@"lolita.jpeg", @"youth.jpg", @"yuan.jpg", @"youth.jpg", @"yuan.jpg", @"youth.jpg", @"yuan.jpg", @"youth.jpg", @"yuan.jpg", @"youth.jpg", @"yuan.jpg"];
    _viewArray = [[NSMutableArray alloc] initWithCapacity:3];
    for (NSInteger i = 0; i < imageNameArray.count; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth * i, 0, kScreenWidth, kScreenHeight)];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.contents = (__bridge id)[UIImage imageNamed:imageNameArray[i]].CGImage;
        view.layer.contentsGravity = kCAGravityResizeAspect;
        [_viewArray addObject:view];
    }
    ZYStoryTransitiomScrollView * scrollView = [[ZYStoryTransitiomScrollView alloc] initWithFrame:UIScreen.mainScreen.bounds];
    [self.view addSubview:scrollView];
    scrollView.viewArray = self.viewArray;
    
}

- (void)testPopVC {
    _btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame = CGRectMake(150, 100, 100, 100);
    
    [_btn setTitle:@"PopOver" forState:UIControlStateNormal];
    _btn.backgroundColor = [UIColor orangeColor];
    [_btn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:_btn];
}

- (void)pop{
    
    //  初始化弹出控制器
    UIViewController *vc = [UIViewController new];
    
    //  背景色
    vc.view.backgroundColor = [[UIColor yellowColor] colorWithAlphaComponent:0.5];
    
    //  弹出视图的显示样式
    vc.modalPresentationStyle = UIModalPresentationPopover;
    
    //  1、弹出视图的大小
    vc.preferredContentSize = CGSizeMake(300, 300);
    
    //  弹出视图的代理
    vc.popoverPresentationController.delegate = self;
    
    //  弹出视图的参照视图、从哪弹出
    vc.popoverPresentationController.sourceView = _btn;
    
    //  弹出视图的尖头位置：参照视图底边中间位置
    vc.popoverPresentationController.sourceRect = _btn.bounds;
    
    //  弹出视图的箭头方向
    vc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
    
    //  弹出
    [self presentViewController:vc animated:YES completion:nil];
}

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
    return YES;
}

-(UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

- (void) handlePan: (UIPanGestureRecognizer *)pan{
    UIView *gesView = pan.view;
    CGPoint point = [pan translationInView:gesView];
    [pan setTranslation:CGPointZero inView:gesView];
    gesView.center = CGPointMake(gesView.center.x + point.x, gesView.center.y);
    
    // -0.0 -> -1.0
    CGFloat ratio = gesView.frame.origin.x / (gesView.frame.size.width * 0.5);
    // 0 -> -M_PI_2
    self.layerView1.layer.transform = [self transformWithRatio:ratio];
    // M_PI_2 -> 0
    self.layerView2.layer.transform = [self transformWithRatio:1+ratio];
    if (pan.state == UIGestureRecognizerStateEnded) {
        NSLog(@"velocity = %@", NSStringFromCGPoint([pan velocityInView:gesView]));
    }
}

- (CATransform3D)transformWithRatio:(CGFloat)ratio {
//    NSLog(@"real ratio = %@", @(ratio));
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -1.0f / 1500;
    transform = CATransform3DRotate(transform, M_PI_2 * 1 * ratio, 0, 1, 0);
    return transform;
}


@end
